package system.events;

import java.util.Random;

import models.EventsType;

import system.eventBuffer.EventBuffer;
import system.main.SystemDataStructure;

public class EventsGenerator extends Thread {

	private boolean run = true;
	Random randomEvent = new Random();
	EventBuffer eventBuffer;
	SoftwareEvent softwareEvent;
	EventsType eventsType;

	public EventsGenerator(SystemDataStructure ds) {
		this.eventsType = ds.getEventType();
		this.eventBuffer = ds.getEventBuffer();

	}

	public void pauseThread() {
		run = false;
	}

	public void resumeThread() {
		run = true;
	}

	public void run() {
		while (run) {
			for (int k = 0; k < 1000; k++) {
				if (eventBuffer.getSize() >= 150000) {
					break;
				} else {

					int i = randomEvent.nextInt(200);
					switch (i) {
					case 1:
						// software alert
						switch (randomEvent.nextInt(2)) {
						case 0:
							eventBuffer.offer(new SoftwareEvent(
									SoftwareEventType.io,
									SoftwareMessage.IOMessage1));
							break;
						case 1:
							eventBuffer.offer(new SoftwareEvent(
									SoftwareEventType.storage,
									SoftwareMessage.StorageMessage1));
							break;
						}
						eventsType.addAlert();
						break;
					case 2:// hardware alert
						eventBuffer.offer(new HardwareEvent(
								HardwareEventState.dead, 100));
						eventsType.addAlert();
						break;
					case 3:
					case 4:
						// software warnning
						switch (randomEvent.nextInt(2)) {
						case 0:
							eventBuffer.offer(new SoftwareEvent(
									SoftwareEventType.io,
									SoftwareMessage.IOMessage2));
							break;
						case 1:
							eventBuffer.offer(new SoftwareEvent(
									SoftwareEventType.storage,
									SoftwareMessage.StorageMessage2));
							break;
						}
						eventsType.addWarning();
						break;
					case 5:
					case 6:
						// hardware warnning
						eventBuffer.offer(new HardwareEvent(85));
						eventsType.addWarning();
						break;
					default: {
						switch (randomEvent.nextInt(2)) {
						case 0:// normal software event
							switch (randomEvent.nextInt(2)) {
							case 0:
								eventBuffer.offer(new SoftwareEvent(
										SoftwareEventType.io,
										SoftwareMessage.IOMessage3));
								break;
							case 1:
								eventBuffer.offer(new SoftwareEvent(
										SoftwareEventType.storage,
										SoftwareMessage.StorageMessage3));
								break;
							}
							break;
						case 1:// normal hardware event
							eventBuffer.offer(new HardwareEvent(40));
							break;
						}
						eventsType.addNormal();
						break;
					}
					}
				}
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
